'
' DotNetNuke Ita - http://dotnetnukeita.blogspot.com
' Daniele Lembo
'


Imports System
Imports System.Configuration
Imports System.Data
Imports System.XML
Imports System.Web
Imports System.Collections.Generic
Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Services.Search
Imports DotNetNuke.Common.Utilities.XmlUtils
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Modules.Definitions
Imports DotNetNuke.Security
Imports DotNetNuke.Common.Utilities
Imports DotNetNukeIta.Modules.RSSNuke.Database
Imports DotNetNukeIta.Modules.RSSNuke.Utils
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions

Namespace DotNetNukeIta.Modules.RSSNuke.Controllers

    ''' <summary>
    ''' The Controller class for RSSNuke
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class RSSNukeController



#Region "Variabili Private"
        Private _PremessoDemo As Boolean
#End Region

#Region "Costanti pubbliche"
        Public Const PERMISSIONCODE As String = "RSSNuke"

        Public Const PERMESSODEMO As String = "PERMESSODEMO"
#End Region

#Region "Private Methods"

        Private Function ProcessRSSItem(ByVal TabModuleId As Integer) As RSSFeed

            Dim objRSSFeed As New RSSFeed
            Dim listRSSItem As New List(Of RSSItem)
            Dim objRSSItem As RSSItem

            Try

                Dim objModules As New Entities.Modules.ModuleController
                objRSSFeed.Url = CType(objModules.GetTabModuleSettings(TabModuleId)("rssfeeds"), String)
                Dim numOfFeeds As Integer = 5
                If IsNumeric(objModules.GetTabModuleSettings(TabModuleId)("numOfFeeds")) Then
                    numOfFeeds = CType(objModules.GetTabModuleSettings(TabModuleId)("numOfFeeds"), Integer)
                End If
                Dim newWindow As Boolean = CType(objModules.GetTabModuleSettings(TabModuleId)("newWindow"), Boolean)
                Dim numOfDescrChars As Integer = 200
                If IsNumeric(objModules.GetTabModuleSettings(TabModuleId)("numOfDescrChars")) Then
                    numOfDescrChars = CType(objModules.GetTabModuleSettings(TabModuleId)("numOfDescrChars"), Integer)
                End If
                Dim strTarget As String = "_self"
                If newWindow Then
                    strTarget = "_new"
                End If

                If objRSSFeed.Url Is Nothing Then
                    Throw New UriFormatException("Module needs to be configured.")
                End If
                Dim myRequest As WebRequest = System.Net.WebRequest.Create(objRSSFeed.Url)
                Dim myResponse As WebResponse = myRequest.GetResponse()
                Dim rssStream As Stream = myResponse.GetResponseStream()
                Dim rssDoc As New XmlDocument()
                rssDoc.Load(rssStream)

                Dim rssItems As XmlNodeList = rssDoc.SelectNodes("rss/channel/item")
                Dim rssFeed As XmlNode = rssDoc.SelectNodes("rss/channel")(0)

                Dim i As Integer
                Dim rows As Integer

                If rssItems.Count > numOfFeeds Then
                    rows = numOfFeeds - 1
                Else
                    rows = rssItems.Count - 1
                End If

                objRSSFeed.Title = rssFeed.SelectSingleNode("title").InnerText
                objRSSFeed.Url = rssFeed.SelectSingleNode("link").InnerText
                If CType(objModules.GetTabModuleSettings(TabModuleId)("dateFormat"), String) <> "" Then
                    objRSSFeed.DateTimeFormat = CType(objModules.GetTabModuleSettings(TabModuleId)("dateFormat"), String)
                End If

                For i = 0 To rows
                    Dim rssDetail As XmlNode
                    objRSSItem = New RSSItem
                    rssDetail = rssItems.Item(i).SelectSingleNode("title")
                    If Not rssDetail Is Nothing Then
                        objRSSItem.Title = rssDetail.InnerText
                    Else
                        objRSSItem.Title = ""
                    End If
                    rssDetail = rssItems.Item(i).SelectSingleNode("link")
                    If Not rssDetail Is Nothing Then
                        objRSSItem.Link = rssDetail.InnerText
                    Else
                        objRSSItem.Link = ""
                    End If
                    rssDetail = rssItems.Item(i).SelectSingleNode("description")
                    If Not rssDetail Is Nothing Then
                        Dim tempText As String
                        tempText = rssDetail.InnerText
                        objRSSItem.Description = Left(StringFunctions.RemoveHTMLTags(tempText), numOfDescrChars) & " ..."
                    Else
                        objRSSItem.Description = ""
                    End If

                    objRSSItem.Target = strTarget

                    rssDetail = rssItems.Item(i).SelectSingleNode("pubDate")
                    Dim itemDate As DateTime = Date.Today
                    If Not rssDetail Is Nothing Then
                        If IsDate(rssDetail.InnerText) Then
                            itemDate = Date.Parse(rssDetail.InnerText)
                        End If
                    Else
                        itemDate = Now()
                    End If

                    objRSSItem.CreatedDate = Date.SpecifyKind(itemDate, DateTimeKind.Utc).ToString(objRSSFeed.DateTimeFormat)

                    listRSSItem.Add(objRSSItem)
                Next
            Catch uriex As UriFormatException

                listRSSItem = Nothing
                objRSSFeed.Feeds = listRSSItem
                objRSSFeed.Title = uriex.Message
                Return objRSSFeed

            Catch webex As WebException

                listRSSItem = Nothing
                objRSSFeed.Feeds = listRSSItem
                objRSSFeed.Title = webex.Message
                Return objRSSFeed

            Catch ex As Exception
                listRSSItem = Nothing
                objRSSFeed = Nothing
                Throw ex
            End Try

            objRSSFeed.Feeds = listRSSItem

            Return objRSSFeed
        End Function


#End Region

#Region "Public Methods"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' gets an object from the database
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="TabModuleId">The Id of the module</param>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Function GetRSSFeeds(ByVal TabModuleId As Integer) As RSSFeed

            Return ProcessRSSItem(TabModuleId)

        End Function

#End Region

#Region "Optional Interfaces"


#End Region

    End Class
End Namespace
